package edu.infomatik.tundra.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumSelectModel;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.logic.LifeStages;
import edu.infomatik.tundra.logic.USTaskBean;
import edu.infomatik.tundra.logic.UStoryTypes;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.WorkKinds;
import edu.infomatik.tundra.logic.UStoryBean;
import edu.infomatik.tundra.logic.security.SystemAccessBean;
import edu.infomatik.tundra.model.ProjectIdEncoder;
import edu.infomatik.tundra.model.ProjectIdSelectModel;
import edu.infomatik.tundra.model.StoryIdEncoder;
import edu.infomatik.tundra.model.StoryIdSelectModel;
import edu.infomatik.tundra.data.DataUtils;

@SuppressWarnings("unused")
public class CreateTask {
	@SessionState
	@Property
	private USTaskBean task;
	
	@SessionState
	private SystemAccessBean user;
	private boolean userExists;
	@Inject
	private Messages messages;
	@SessionState
	private Boolean newWork;
	
	private WorkBean work;
	private UStoryBean story;
	private LifeStages selectedLifeStage;
	@Property
	private String lifeStageLabel;
	@Property
	private String projectIdLabel;
	@Property
	private String storyIdLabel;
	@Property
	private String addRaw;

	public LifeStages getSelectedLifeStage() {
		return selectedLifeStage;
	}
	public void setSelectedLifeStage(LifeStages selectedLifeStage) {
		this.selectedLifeStage = selectedLifeStage;
		task.setLifeStage(selectedLifeStage);
	}
	public String getSelectKind(){
		return messages.get("WorkKinds."+WorkKinds.get(task.getKind()).getName());
	}
	public SelectModel gettKind() {
		return new EnumSelectModel(WorkKinds.class, messages);
	}
	public SelectModel gettLifeStage() {
		return new EnumSelectModel(LifeStages.class, messages);
	}

	String onActivate(){
//		System.out.println(DAOtundra.getInstance().getMaxId());
		addRaw="";
		storyIdLabel="<br/>.";
		if (!userExists) return "Index";
		if (null == task || task.getId()==0 || newWork.booleanValue()){
			task = new USTaskBean();
			task.setId(DAOtundra.getInstance().getMaxId()+1);
			task.setDateCreated(DataUtils.getNow());
		}else{
			lifeStageLabel = "(<i>"+messages.get("LifeStages."+LifeStages.get(task.getLifeStage()).getName())+"</i>)";
			WorkBean b = new WorkBean();
			// adding text to projectId-Label 
			if (task.getProjectId()>0){
				b.setId(task.getProjectId());
				projectIdLabel="<br/>(<i>"+((WorkBean)DAOtundra.getInstance().getBeanById(b)).getName()+"</i>)";
				addRaw="<br/>";
			} else projectIdLabel="";
			// adding text to storyId-Label 
			if (task.getStoryId()>0){
				b.setId(task.getStoryId());
				storyIdLabel="<br/>(<i>"+((WorkBean)DAOtundra.getInstance().getBeanById(b)).getName()+"</i>)";	
			}
		}
		newWork=new Boolean(false);
		return null;
	}
	public long getTaskId(){
		return task.getId();
	}
	void onSubmitFromTask(){
		DAOtundra.getInstance().update(task);
	}
	// Begin: methods for selectProject
	public List<WorkBean> getAllProjects(){
		List<WorkBean> wbList= new ArrayList<WorkBean>();
		for (IBean wb: DAOtundra.getInstance().getAllBeans(new WorkBean())){
			if (((WorkBean)wb).getKind()==WorkKinds.PROJECT_VALUE){
				wbList.add((WorkBean)wb);
			}
		}
		return wbList;
	}
	
	public SelectModel getProjectIdModel(){
		return new ProjectIdSelectModel(getAllProjects());
	}
	public ValueEncoder<WorkBean> getProjectIdEncoder(){
		return new ProjectIdEncoder();
	}
	
	public WorkBean getProjectIdSelection(){
		return work;
	}
	public void setProjectIdSelection(WorkBean selectedProject){
		work = selectedProject;
		task.setProjectId(selectedProject.getId());
	}
	// END: methods for selectProject
	
	// Begin: methods for selectStory
	public List<UStoryBean> getAllStories(){
		List<UStoryBean> storyList= new ArrayList<UStoryBean>();
		for (IBean st: DAOtundra.getInstance().getAllBeans(new UStoryBean())){
			if (((UStoryBean)st).getKind()==WorkKinds.USTORY_VALUE){
				storyList.add((UStoryBean)st);
			}
		}
		return storyList;
	}
	
	public SelectModel getStoryIdModel(){
		return new StoryIdSelectModel(getAllStories());
	}
	
	public ValueEncoder<UStoryBean> getStoryIdEncoder(){
		return new StoryIdEncoder();
	}
	
	public UStoryBean getStoryIdSelection(){
		return story;
	}
	
	public void setStoryIdSelection(UStoryBean selectedStory){
		story = selectedStory;
		task.setStoryId(selectedStory.getId());
	}
	// END: methods for selectStory

}
